import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/model/category_entity.dart';
import 'package:mall/modules/sc/category_sc.dart';
import 'package:mall/ui/page/goods/category/category_goods_widget.dart';
import 'package:mall/utils/tabbar/app_tabbar.dart';

import '../../../modules/sc/goods_sc.dart';

class HomeCategoryGoodsPage extends StatefulWidget {
  final String title;
  final int id;

  HomeCategoryGoodsPage(this.title, this.id);

  @override
  _HomeCategoryGoodsPageState createState() => _HomeCategoryGoodsPageState();
}

class _HomeCategoryGoodsPageState extends State<HomeCategoryGoodsPage> with TickerProviderStateMixin {
  // CategoryGoodsViewModel _categoryGoodsViewModel = CategoryGoodsViewModel();
  late final TabController _tabController;
  final sc_categoryGoodsList = Get.put(CategoryGoodsListSc());
  final sc_categoryTitle = Get.put(CategoryTitleSc());
  @override
  void initState() {
    super.initState();
    // _categoryGoodsViewModel.queryCategoryName(widget.id);
    sc_categoryGoodsList.categoryId = widget.id;
    sc_categoryGoodsList.refreshData();
    sc_categoryTitle.categoryId = widget.id;
    sc_categoryTitle.refreshData().then((value) {
      final model = sc_categoryTitle.data.value;
      _tabController = TabController(
          length: model == null || model.brotherCategory == null ? 0 : model.brotherCategory!.length, vsync: this);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text(widget.title),
          centerTitle: true,
          bottom: PreferredSize(
            preferredSize: Size.fromHeight(60),
            child: Material(
              color: AppColors.C_FFFFFF,
              child: Obx(() {
                final model = sc_categoryTitle.data.value;
                if (model == null || model.brotherCategory == null) {
                  return const SizedBox();
                }
                return AppTabBar(
                    isScrollable: true,
                    controller: _tabController,
                    indicatorColor: AppColors.C_FF5722,
                    labelColor: AppColors.C_FF5722,
                    unselectedLabelColor: AppColors.C_999999,
                    tabAlignment: TabAlignment.start,
                    padding: const EdgeInsets.only(bottom: 15, top: 15, left: 8),
                    labelStyle: FMTextStyle.color_999999_size_13,
                    unselectedLabelStyle: FMTextStyle.color_ff5722_size_13,
                    tabs: tabBars(model.brotherCategory == null ? [] : model.brotherCategory!));
              }),
            ),
          )),
      body: Obx(() {
        final model = sc_categoryTitle.data.value;
        if (model == null || model.brotherCategory == null) {
          return const SizedBox();
        }
        return TabBarView(
            controller: _tabController,
            children: tabBarViews(model.brotherCategory == null ? [] : model.brotherCategory!));
      }),
    );
  }

  List<Widget> tabBars(List<CategoryEntity> titles) {
    List<Widget> titlesWidget = [];
    for (var item in titles) {
      titlesWidget.add(Tab(text: item.name));
    }
    return titlesWidget;
  }

  List<Widget> tabBarViews(List<CategoryEntity> titles) {
    List<Widget> contentWidgets = [];
    for (var item in titles) {
      contentWidgets.add(CategoryGoodsWidget(item.id ?? 0));
    }
    return contentWidgets;
  }
}
